/*
 * NumberList.java
 *
 * Created on May 15, 2008, 2:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;
import java.util.*;

public class NumberList extends List implements CommandListener{

private SMSClient client;
private PhoneBook pBook;
private static final Command addCmd = new Command("Add", Command.OK, 1);
private static final Command allCmd = new Command("Select ALL", Command.OK, 1);
private static final Command callCmd = new Command("Clear ALL", Command.OK, 1);
private static final Command backCmd = new Command("Cancel", Command.CANCEL, 1);

public NumberList(SMSClient client, PhoneBook pBook) {

super("Add Number List", List.MULTIPLE);

this.client = client;
this.pBook = pBook;

this.addCommand(addCmd);
this.addCommand(backCmd);
this.addCommand(allCmd);
this.addCommand(callCmd);
this.setCommandListener(this);

String[] contact = this.pBook.loadBook();

for(int i = 0; i < contact.length; i++){

int idx = contact[i].lastIndexOf(';');
contact[i] = contact[i].substring(0, idx);
contact[i] = contact[i].replace(';', ':');
this.append(contact[i], null);
}
}

public void commandAction(Command c, Displayable d) {

if(c == addCmd){

boolean[] selected = new boolean[size()];
Vector v = new Vector();
getSelectedFlags(selected);
for (int i = 0; i < selected.length; ++i) {
if (selected[i]) {

v.addElement(getString(i));
}
}

String[] con = new String[v.size()];
for(int i = 0; i < con.length; i++){
con[i] = v.elementAt(i).toString();
}
this.client.addNumber(con);
Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.client);

}else if(c == allCmd){

for(int i = 0; i < size(); i++){
this.setSelectedIndex(i, true);
}
}else if(c == callCmd){

for(int i = 0; i < size(); i++){
this.setSelectedIndex(i, false);
}
}

else if(c == backCmd){

Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.client);

}
}
}

